package com.small.auth.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 小程序的登陆密码
 */
@Service
@Slf4j
public class WxWeChatPawService {

//    @Autowired
//    private IAuUserService iAuUserService;


    //小程序登录
    public UserDetails loadUserByPaw(Map<String, String> param) {
        String openid = param.get("openid");
        String username = param.get("username");
        String password = param.get("password");

//        AuUser byOpenId = iAuUserService.findByOpenId(openid);
//        AuUser byOpenId = iAuUserService.getById("");
//        if (byOpenId == null) {//没有openid
//            throw new BusinessException("openid错误");
//        }
//        //没有绑定用户
//        if (byOpenId.getUserName() == null) {
//            AuUser userByName = iAuUserService.findUserByName(username);
//            userByName.setOpenId(openid);
//            userByName.setSessionKey(byOpenId.getSessionKey());
//            iAuUserService.removeById(byOpenId);
//            byOpenId = userByName;
//
//        }
//        byOpenId.setLoginStatus(1);//设置登录保持状态
//        iAuUserService.saveOrUpdate(byOpenId);
//        //设置权限
        Set<GrantedAuthority> set = new HashSet<>();
//        List<AuPermission> permissions = iAuUserService.getUserPermission(byOpenId.getId());
//        permissions.forEach(p -> {
//            GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(p.getPermissionCode());
//            set.add(grantedAuthority);
//        });
        return new User("", "", set);
//        return CustomUser.builder().password(password).username(byOpenId.getUserName()).openid(byOpenId.getOpenId()).auUser(byOpenId).authorities(set).build();
    }
}
